home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / xWindows definition files / TextPrint.p < prev    next >
Encoding:
Text File  |  1993-09-14  |  6.7 KB  |  261 lines  |  [TEXT/PJMM]

  1. unit TextPrint;
  2.  
  3. {  This unit defines simple procedures for printing text.  To use it you should: }
  4. {                1) Include a Page Setup command in your File menu, and call DoPageSetup }
  5. {                     in response to it. }
  6. {                2) Add the file PrintTraps.p (from the Interfaces folder in THINK Pascal) }
  7. {                    to your project. }
  8. {                3) Set PrintDataRecord := NIL in your application initialization procedure (so }
  9. {                    that the procedures in this unit will know that it needs to be initialized }
  10. {                    when they are first called. }
  11.  
  12. interface
  13.  
  14. uses
  15.     PrintTraps;
  16.  
  17. var
  18.     PrintDataRecord: THPrint;
  19.  
  20. procedure DoPageSetup;
  21.  
  22. procedure SimplePrintText (theText: CharsHandle);
  23.  
  24. procedure PrintText (theText: CharsHandle;
  25.                             textLength: integer;
  26.                             header: string;
  27.                             theFont, theFontSize: integer;
  28.                             inchesWide: real);
  29.  
  30.  
  31. implementation
  32.  
  33. var
  34.     printDlg: DialogRecord;
  35.     printDlgPtr: dialogPtr;
  36.  
  37. procedure TellUser (message: string);
  38.     var
  39.         bttn: integer;
  40.     begin
  41.         ParamText(message, '', '', '');
  42.         bttn := NoteAlert(129, nil);
  43.         if bttn = -1 then
  44.             Sysbeep(5);
  45.     end;
  46.  
  47. procedure initPrint (var good: boolean);
  48.     begin
  49.         PrOpen;
  50.         if PrError <> noErr then begin
  51.                 TellUser('Unable to open printer driver.  (Have you used theChooser to select a printer?)');
  52.                 good := false;
  53.             end;
  54.         if PrintDataRecord <> nil then
  55.             good := true
  56.         else begin
  57.                 PrintDataRecord := THPrint(NewHandle(SizeOf(TPrint)));
  58.                 if MemError <> noErr then begin
  59.                         TellUser('You do not have enough memory available for printing.');
  60.                         good := false
  61.                     end
  62.                 else begin
  63.                         PrintDefault(PrintDataRecord);
  64.                         good := true
  65.                     end;
  66.             end;
  67.     end;
  68.  
  69. procedure DoPageSetup;
  70.     var
  71.         good: boolean;
  72.     begin
  73.         initPrint(good);
  74.         if good then
  75.             good := PrStlDialog(PrintDataRecord);
  76.         PRClose;
  77.     end;
  78.  
  79.  
  80. procedure SimplePrintText (theText: CharsHandle);
  81.     begin
  82.         PrintText(theText, GetHandleSize(Handle(theText)), '', geneva, 12, 0);
  83.     end;
  84.  
  85.  
  86. procedure PrintText (theText: CharsHandle;
  87.                                 textLength: integer;
  88.                                 header: string;
  89.                                 theFont, theFontSize: integer;
  90.                                 inchesWide: real);
  91.     var
  92.         good: boolean;
  93.         savePort: GrafPtr;
  94.         currentLine, currentWord: string;
  95.         currentPageHeight: integer;
  96.         i: integer;
  97.         hRes, vRes: integer;
  98.         lineHeight: integer;
  99.         pageWidth, pageHeight: integer;
  100.         margin: integer;
  101.         prPort: TPPrPort;
  102.         status: TPrStatus;
  103.         fInfo: FontInfo;
  104.         pageNo: integer;
  105.         left, right: integer;
  106.         startingPage: boolean;
  107.         BlankCt: integer;
  108.     procedure StartPage;
  109.         var
  110.             i: integer;
  111.             str: string;
  112.             time: DateTimeRec;
  113.             shoveRight: boolean;
  114.         begin
  115.             if currentPageHeight <> -2 then begin
  116.                     PrOpenPage(prPort, nil);
  117.                     TextFont(theFont);
  118.                     TextSize(theFontSize);
  119.                 end;
  120.             PageNo := pageNo + 1;
  121.             if header = '' then
  122.                 CurrentPageHeight := 0
  123.             else begin
  124.                     GetTime(time);
  125.                     i := 1;
  126.                     str := '';
  127.                     shoveRight := false;
  128.                     MoveTo(left, finfo.ascent);
  129.                     while i <= length(header) do begin
  130.                             if (header[i] <> '\') | (i = length(header)) then
  131.                                 str := Concat(str, header[i])
  132.                             else begin
  133.                                     i := i + 1;
  134.                                     if header[i] = 'p' then
  135.                                         str := StringOf(str, pageNo : 1)
  136.                                     else if header[i] = 'd' then
  137.                                         str := stringOf(str, time.month : 1, '/', time.day : 1, '/', time.year mod 100 : 1)
  138.                                     else if header[i] = 't' then
  139.                                         str := stringOf(str, time.hour : 1, ':', time.minute : 1, ':', time.second : 1)
  140.                                     else if header[i] = 'r' then begin
  141.                                             if str <> '' then begin
  142.                                                     TextFace([bold]);
  143.                                                     DrawString(str);
  144.                                                     str := '';
  145.                                                 end;
  146.                                             shoveRight := true;
  147.                                         end
  148.                                     else
  149.                                         Str := Concat(str, header[i])
  150.                                 end;
  151.                             i := i + 1;
  152.                         end;
  153.                     TextFace([bold]);
  154.                     if shoveRight then
  155.                         MoveTo(right - StringWidth(str), fInfo.ascent)
  156.                     else
  157.                         MoveTo(left, finfo.ascent);
  158.                     DrawString(str);
  159.                     TextFace([]);
  160.                     CurrentPageHeight := lineHeight * 2;
  161.                 end;
  162.             startingPage := true;
  163.         end;
  164.     procedure DumpLine;
  165.         begin
  166.             if (currentLine <> '') | not startingPage then begin
  167.                     MoveTo(left, CurrentPageHeight + fInfo.ascent);
  168.                     DrawString(currentLine);
  169.                     CurrentPageHeight := CurrentPageHeight + lineHeight;
  170.                     currentLine := '';
  171.                     if currentpageHeight + FInfo.ascent > pageHeight then begin
  172.                             PRClosePage(prPort);
  173.                             CurrentPageHeight := -1;
  174.                         end;
  175.                 end;
  176.         end;
  177.     procedure DumpWord;
  178.         var
  179.             j: integer;
  180.         begin
  181.             if StringWidth(currentLine) + StringWidth(currentWord) > margin then
  182.                 DumpLine;
  183.             currentLine := concat(currentLine, currentWord);
  184.             currentWord := '';
  185.             if (currentLine <> '') | (BlankCt > 2) then
  186.                 for j := 1 to BlankCt do
  187.                     currentLine := Concat(currentLine, ' ');
  188.             BlankCt := 0;
  189.         end;
  190.     procedure PutChar (ch: char);
  191.         begin
  192.             if CurrentPageHeight < 0 then
  193.                 StartPage
  194.             else
  195.                 startingPage := false;
  196.             if (ch = chr(13)) | (ch = chr(3)) then begin
  197.                     if currentWord <> '' then
  198.                         DumpWord;
  199.                     BlankCt := 0;
  200.                     DumpLine;
  201.                 end
  202.             else if ch = ' ' then
  203.                 BlankCt := BlankCt + 1
  204.             else begin
  205.                     if (BlankCt > 0) | (StringWidth(currentWord) + charWidth(ch) > margin) then
  206.                         DumpWord;
  207.                     currentWord := Concat(currentWord, ch);
  208.                 end;
  209.         end;
  210.     begin
  211.         initPrint(good);
  212.         if not good then
  213.             exit(PrintText);
  214.         good := PrJobDialog(printDataRecord);
  215.         if not good then
  216.             EXIT(printText);  { user canceled }
  217.         GetPort(savePort);
  218.         printDlgPtr := GetNewDialog(300, @printDlg, pointer(-1));
  219.         DrawDialog(printDlgPtr);
  220.         prPort := PrOpenDoc(PrintDataRecord, nil, nil);
  221.         PrOpenPage(prPort, nil);
  222.         TextFont(theFont);
  223.         TextSize(theFontSize);
  224.         GetFontInfo(fInfo);
  225.         lineHeight := fInfo.ascent + fInfo.descent + fInfo.leading;
  226.         hRes := printDataRecord^^.prInfo.iHRes;
  227.         vRes := printDataRecord^^.prInfo.iVRes;
  228.         pageHeight := printDataRecord^^.prInfo.rPage.bottom - vRes;  { allow 1/2 inch margins all around }
  229.         pageWidth := printDataRecord^^.prInfo.rPage.right - hRes;
  230.         if inchesWide < 1 then
  231.             margin := pageWidth
  232.         else
  233.             margin := round(inchesWide * hRes);
  234.         if margin > pageWidth then
  235.             margin := pageWidth;
  236.         left := hRes div 2;
  237.         right := left + margin;
  238.         currentLine := '';
  239.         currentWord := '';
  240.         currentPageHeight := -2;  { -2 tells start page not to open a new page }
  241.         PageNo := 0;
  242.         BlankCt := 0;
  243.         for i := 0 to textLength - 1 do
  244.             PutChar(theText^^[i]);
  245.         if CurrentPageHeight <> -1 then begin
  246.                 DumpWord;
  247.                 DumpLine;
  248.                 if CurrentPageHeight <> -1 then
  249.                     PrClosePage(prPort);
  250.             end;
  251.         PrCloseDoc(prPort);
  252.         if (printDataRecord^^.prJob.bJDocLoop = bSpoolLoop) & (PrError = noErr) then begin
  253.                 PrPicFile(printDataRecord, nil, nil, nil, status);
  254.             end;
  255.         CloseDialog(printDlgPtr);
  256.         SetPort(savePort);
  257.         PRClose
  258.     end;
  259.  
  260.  
  261. end.